﻿// Copyright (c) 2017 GlassGrass

using System.Collections.Generic;
using System.Linq;
using UniRx;
using UnityEngine;



namespace GlassGrass.Unity.GuiFramework
	{
	/// <summary> Layouting container. </summary>
	/// <remarks>
	///     <para> In manual mode this class uses GUI.BeginGroup/EndGroup. </para>
	///     <para> In auto mode this class uses GUILayout.BeginArea/EndArea. </para> 
	/// </remarks>
	public class Container : ContainerBase
		{

		/// <summary></summary>
		/// <param name="config"> [Non-Nullable] </param>
		/// <exception cref="System.ArgumentNullException"></exception>
		public Container(GuiConfig config) : base( config )
			{
			}



		#region override methods
		
		/// <summary> Define what to choose style for this from skin. </summary>
		/// <param name="skin"></param>
		/// <returns></returns>
		protected override GUIStyle ChooseStyle(GUISkin skin)
			=> skin.box;



		/// <summary> Define how to layout manually itself on rendering frame. </summary>
		/// <param name="parentSkin"></param>
		public override void LayoutManual(GUISkin parentSkin)
			{
			var skin = SelectValidSkin( parentSkin );
			GUI.BeginGroup( Rect, Content, SelectValidStyle( parentSkin ) );
			foreach( var child in Children.Where( c => c.Visible ) )
				child.LayoutManual( skin );
			GUI.EndGroup();
			}



		/// <summary> Define how to layout automatically itself on rendering frame.  </summary>
		/// <param name="parentSkin"></param>
		public override void LayoutAuto(GUISkin parentSkin)
			{
			var skin = SelectValidSkin( parentSkin );
			GUILayout.BeginArea( Rect, Content, SelectValidStyle( parentSkin ) );
			foreach( var child in Children.Where( c => c.Visible ) )
				child.LayoutAuto( skin );
			GUILayout.EndArea();
			}

		#endregion

		}
	}
